<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <!-- <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px"> -->
      <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="部门名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="上级部门id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="上级部门id"></el-input>
    </el-form-item>
    <el-form-item label="树结构" prop="treePath">
      <el-input v-model="dataForm.treePath" placeholder="树结构"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sortValue">
      <el-input v-model="dataForm.sortValue" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="leader">
      <el-input v-model="dataForm.leader" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="状态（1正常 0停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（1正常 0停用）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      
<el-input v-model="dataForm.updateTime" placeholder="修改時間"></el-input>
    </el-form-item>
    <el-form-item label="删除标记（0:不可用 1:可用）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除标记（0:不可用 1:可用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          parentId: '',
          treePath: '',
          sortValue: '',
          leader: '',
          phone: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          name: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '上级部门id不能为空', trigger: 'blur' }
          ],
          treePath: [
            { required: true, message: '树结构不能为空', trigger: 'blur' }
          ],
          sortValue: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          leader: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（1正常 0停用）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除标记（0:不可用 1:可用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/system/sysdept/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.name = data.sysDept.name
                this.dataForm.parentId = data.sysDept.parentId
                this.dataForm.treePath = data.sysDept.treePath
                this.dataForm.sortValue = data.sysDept.sortValue
                this.dataForm.leader = data.sysDept.leader
                this.dataForm.phone = data.sysDept.phone
                this.dataForm.status = data.sysDept.status
                this.dataForm.createTime = data.sysDept.createTime
                this.dataForm.updateTime = data.sysDept.updateTime
                this.dataForm.isDeleted = data.sysDept.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        // alert("调用了这个form方法请继续这个测试这个")
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/sysdept/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              header:{
                    "content-type":'application/json'
              },
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'parentId': this.dataForm.parentId,
                'treePath': this.dataForm.treePath,
                'sortValue': this.dataForm.sortValue,
                'leader': this.dataForm.leader,
                'phone': this.dataForm.phone,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            }).catch(error=>{
              console.log(error);
              this.visible = false
                    this.$emit('refreshDataList')
            })
          }
        })
      }
    }
  }
</script>
